package com.zzyl.nursing.service.impl;

import java.util.List;

import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.mapper.DeviceMapper;
import com.zzyl.nursing.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.AlertDataMapper;
import com.zzyl.nursing.domain.AlertData;
import com.zzyl.nursing.service.IAlertDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;
import com.zzyl.framework.interceptor.MyMetaObjectHandler;
/**
 * 报警数据Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-05
 */
@Service
@Slf4j
public class AlertDataServiceImpl extends ServiceImpl<AlertDataMapper,AlertData> implements IAlertDataService
{
    @Autowired
    private AlertDataMapper alertDataMapper;
    @Autowired
    private DeviceMapper deviceMapper;
    @Autowired
    private UserMapper userMapper;


    /**
     * 查询报警数据
     * 
     * @param id 报警数据主键
     * @return 报警数据
     */
    @Override
    public AlertData selectAlertDataById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询报警数据列表
     * 
     * @param alertData 报警数据
     * @return 报警数据
     */
    @Override
    public List<AlertData> selectAlertDataList(AlertData alertData)
    {
        List<AlertData> alertData1 = alertDataMapper.selectAlertDataList(alertData);
        //对alertData1进行遍历
        for (AlertData alertData2 : alertData1) {
            //获取每一项的iotid
            String iotId = alertData2.getIotId();
            //获取每一项的deviceName字段
            String deviceNameBefore = alertData2.getDeviceName();
            //根据iotId查询设备nodeId
            String deviceName = deviceMapper.selectDeviceNameByIotId(iotId);
            //设置设备名称
            alertData2.setDeviceName(deviceName);
            //将deviceNameBefore设置到nickName字段
            alertData2.setNickname(deviceNameBefore);
            //获取每一项的remark字段
            String remark = alertData2.getRemark();
            if (remark != null && !remark.equals("")) {
                //将remark设置到accessLocation字段
                alertData2.setAccessLocation(remark);

            }


        }
        return alertData1;
    }

    /**
     * 新增报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int insertAlertData(AlertData alertData)
    {
        log.info("新增报警数据：{}",alertData);
        return save(alertData)?1:0;
    }

    /**
     * 修改报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int updateAlertData(AlertData alertData)
    {
        return updateById(alertData)?1:0;
    }

    /**
     * 批量删除报警数据
     * 
     * @param ids 需要删除的报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids))?1:0;
    }

    /**
     * 删除报警数据信息
     * 
     * @param id 报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataById(Long id)
    {
        return removeById(id)?1:0;
    }
    /**
     * 处理设备报警数据
     * @param alertData
     * @return
     */
    public void handlerAlertData(AlertData alertData){
        log.info("处理设备报警数据：{}",alertData);
        //获取当前登录用户id
        Long userId = MyMetaObjectHandler.loadUserId();
        //根据userId查询用户姓名
        String userName = userMapper.selectUserNameById(userId);
        alertData.setProcessorId(userId);
        alertData.setProcessorName(userName);
        //获取

        alertDataMapper.setAlertDataById(alertData);
    }



}
